﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Data;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    public class Indexer
    {
        private LicensingMode _licensingMode;
        private SharePointObjectsDatabase _database;

        public Indexer(LicensingMode licensingMode, SharePointObjectsDatabase database)
        {
            _licensingMode = licensingMode;
            _database = database;
        }

        internal void IndexSiteObjects(IndexSiteObjectList objects, SPSite site, SPWeb web, SPWebApplication webApplication)
        {
            if (web == null || site == null || objects == null || webApplication == null)
                return;
            ObjectReferenceStore.DeleteObjectReferences(site.ID, web.ID, _database);
            IndexLists(objects, web, site, webApplication);
            SiteInfoStore.IndexComplete(site, web, _database);
        }

        internal void IndexWebApplicationObjects(IndexSiteObjectList objects, SPWebApplication webApplication)
        {
            if (webApplication == null)
                return;
            ObjectReferenceStore.DeleteObjectReferences(webApplication.Id, _database);
            IndexWebApplication(objects, webApplication);
        }

        internal void IndexFarmObjects(IndexSiteObjectList objects)
        {
            ObjectReferenceStore.DeleteObjectReferences(SPFarm.Local.Id, _database);
            IndexFarm(objects, SPFarm.Local);
        }

        private void IndexFarm(IndexSiteObjectList objects, SPFarm farm)
        {
            FeatureDefinitionFarmReferences featureReferencesFarm = new FeatureDefinitionFarmReferences(_licensingMode);
            featureReferencesFarm.Add(objects, farm);

            ObjectReferenceStore store = new ObjectReferenceStore(Guid.Empty, Guid.Empty, _licensingMode, _database);
            store.SaveObjectReferences(featureReferencesFarm);
        }

        private void IndexWebApplication(SiteObjectList objects, SPWebApplication webApplication)
        {
            FeatureDefinitionWebApplicationReferences featureReferencesWebApplication = new FeatureDefinitionWebApplicationReferences(_licensingMode);
            featureReferencesWebApplication.Add(objects, webApplication);

            ObjectReferenceStore store = new ObjectReferenceStore(Guid.Empty, Guid.Empty, _licensingMode, _database);
            store.SaveObjectReferences(featureReferencesWebApplication);
        }

        private void IndexLists(SiteObjectList objects, SPWeb web, SPSite site, SPWebApplication webApplication)
        {
            ContentTypeListReferences contentTypeListReferences = new ContentTypeListReferences(_licensingMode);
            SiteColumnListReferences siteColumnListReferences = new SiteColumnListReferences(_licensingMode);
            SiteColumnContentTypeReferences siteColumnContentTypeReferences = new SiteColumnContentTypeReferences(_licensingMode);
            SharePointGroupReferences sharePointGroupReferences = new SharePointGroupReferences(_licensingMode);
            FeatureDefinitionSiteReferences featureReferencesSite = new FeatureDefinitionSiteReferences(_licensingMode);
            FeatureDefinitionWebReferences featureReferencesWeb = new FeatureDefinitionWebReferences(_licensingMode);

            using (SPWeb rootWeb = site.RootWeb)
            {
                if (rootWeb.ID == web.ID)
                {
                    featureReferencesSite.Add(objects, web, site, webApplication);
                }
                featureReferencesWeb.Add(objects, web, site, webApplication);
                sharePointGroupReferences.AddSecurableObject(web, objects, web, site, webApplication, false);
                foreach (SPList list in web.Lists)
                {
                    contentTypeListReferences.Add(list, objects, web, site, webApplication);
                    siteColumnListReferences.Add(list, objects, web, site, webApplication);
                    sharePointGroupReferences.Add(list, objects, web, site, webApplication);
                }
                foreach (SPContentType contentType in web.ContentTypes)
                {
                    contentTypeListReferences.Add(contentType, objects, web, site, webApplication);
                    siteColumnContentTypeReferences.Add(objects, web, contentType, site, webApplication);
                }
            }

            ObjectReferenceStore store = new ObjectReferenceStore(site.ID, web.ID, _licensingMode, _database);
            store.SaveObjectReferences(contentTypeListReferences);
            store.SaveObjectReferences(siteColumnListReferences);
            store.SaveObjectReferences(siteColumnContentTypeReferences);
            store.SaveObjectReferences(sharePointGroupReferences);
            store.SaveObjectReferences(featureReferencesSite);
            store.SaveObjectReferences(featureReferencesWeb);
        }

        internal static void FindAndStartJob()
        {
            SPJobDefinition indexer = null;
            foreach (SPJobDefinition job in SPContext.Current.Site.WebApplication.JobDefinitions)
            {
                if (job.Name == IndexerJobInstaller.INDEXERJOB_FEATURE_ID)
                {
                    indexer = job;
                    break;
                }
            }
            if (indexer != null)
            {
                indexer.Execute(Guid.Empty);
            }
        }

        internal static bool JobIsInstalled()
        {
            foreach (SPJobDefinition job in SPContext.Current.Site.WebApplication.JobDefinitions)
            {
                if (job.Name == IndexerJobInstaller.INDEXERJOB_FEATURE_ID)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
